import os
import time
import re


SART_BUILD_BASE = '\\\\sgpnas02\\v1_5\\Mobile_Builds\\Signaling_Analyzer\\Development\\7.xx'
NA_BUILD_BASE = '\\\\sgpnas02\\v1_5\\Mobile_Builds\\Network_Analyzer'
CMS_BUILD_BASE = '\\\\sgpnas02\\v1_5\\Mobile_Builds\\Signaling_Analyzer\\Development\\7.xx'

PATTERNS = {'NA': 'na[\d\.]+(full|patch)',
            'SART': 'sart[\d\.]+(full|patch)',
            'CMS': 'cms[\d\.]+(full|patch)'
           }

LOG_FILE = os.path.join('%s%slogs' % (os.path.dirname(os.path.abspath(__file__)), os.sep),
                        'update_build_info_%s.log' % time.strftime('%Y%m%d%H%M%S'))

from _log import FileLog
LOG = FileLog(LOG_FILE)

from _db_connection import DatabaseConnection


def _update_sw_table(base_dir, keyword):
    LOG._log('INFO: start _update_sw_table')
    for root, dirs, files in os.walk(base_dir):
        for file in files:
            uppercase_file = file.upper()
            if uppercase_file.endswith('EXE') and re.search(PATTERNS[keyword], uppercase_file, re.I):
                LOG._log('INFO: file found (%s@%s)' % (file, root))
                file_location = root.replace('\\', '\\\\')
                try:
                    (sw_release, sw_build) = _parse_sw_info(file)
                    if keyword == 'SART':
                        _update_sart_build(sw_release, sw_build, file, file_location)
                    if keyword == 'NA':
                        _update_na_build(sw_release, sw_build, file, file_location)
                    if keyword == 'CMS':
                        _update_cms_build(sw_release, sw_build, file, file_location)
                except Exception, e:
                    LOG._log('ERROR: update sw build failed')
                    LOG._log('ERROR: %s' % e)

    LOG._log('INFO: end _update_sw_table')

def _parse_sw_info(file_name):
    LOG._log('INFO: start _parse_sw_info %s' % file_name)
    ret = re.search('(\d+\.\d+)\.(\d+\.\d+)', file_name)
    if ret:
        sw_release = ret.group(1)
        sw_build = '%s.%s' % (sw_release, ret.group(2))
        LOG._log('INFO: end _parse_sw_info %s' % file_name)
        return (sw_release, sw_build)
    else:
        LOG._log('ERROR: _parse_sw_info failed, file_name=%s' % file_name)
        LOG._log('INFO: end _parse_sw_info %s' % file_name)

def _update_cms_build(sw_release, sw_build, file_name, file_location):
    LOG._log('INFO: start _update_cms_build')

    ret = DB_CONN.execute_sql_statement('select * from cms_release where cms_release_id="%s"' % sw_release)
    if ret.rowcount == 0:
        sql_command = 'insert into cms_release (cms_release_id) values ("%s")' % sw_release
        DB_CONN.execute_sql_statement(sql_command)

    ret = DB_CONN.execute_sql_statement('select * from cms_build where cms_build_id="%s"' % sw_build)
    if ret.rowcount == 0:
        sql_command = 'insert into cms_build (cms_build_id, cms_release_id, cms_build_file_name, cms_build_path) values ("%s", "%s", "%s", "%s")' % \
                      (sw_build, sw_release, file_name, file_location)
    else:
        sql_command = 'update cms_build set cms_release_id="%s", cms_build_file_name="%s", cms_build_path="%s" where cms_build_id="%s"' % \
                      (sw_release, file_name, file_location, sw_build)

    DB_CONN.execute_sql_statement(sql_command)

    LOG._log('INFO: end _update_cms_build')

def _update_na_build(sw_release, sw_build, file_name, file_location):
    LOG._log('INFO: start _update_na_build')

    ret = DB_CONN.execute_sql_statement('select * from na_release where na_release_id="%s"' % sw_release)
    if ret.rowcount == 0:
        sql_command = 'insert into na_release (na_release_id) values ("%s")' % sw_release
        DB_CONN.execute_sql_statement(sql_command)

    ret = DB_CONN.execute_sql_statement('select * from na_build where na_build_id="%s"' % sw_build)
    if ret.rowcount == 0:
        sql_command = 'insert into na_build (na_build_id, na_release_id, na_build_file_name, na_build_path) values ("%s", "%s", "%s", "%s")' % \
                      (sw_build, sw_release, file_name, file_location)
    else:
        sql_command = 'update na_build set na_release_id="%s", na_build_file_name="%s", na_build_path="%s" where na_build_id="%s"' % \
                      (sw_release, file_name, file_location, sw_build)

    DB_CONN.execute_sql_statement(sql_command)

    LOG._log('INFO: end _update_na_build')

def _update_sart_build(sw_release, sw_build, file_name, file_location):
    LOG._log('INFO: start _update_sart_build')

    ret = DB_CONN.execute_sql_statement('select * from sart_release where sart_release_id="%s"' % sw_release)
    if ret.rowcount == 0:
        sql_command = 'insert into sart_release (sart_release_id) values ("%s")' % sw_release
        DB_CONN.execute_sql_statement(sql_command)

    ret = DB_CONN.execute_sql_statement('select * from sart_build where sart_build_id="%s"' % sw_build)
    if ret.rowcount == 0:
        sql_command = 'insert into sart_build (sart_build_id, sart_release_id, sart_build_file_name, sart_build_path) values ("%s", "%s", "%s", "%s")' % \
                      (sw_build, sw_release, file_name, file_location)
    else:
        sql_command = 'update sart_build set sart_release_id="%s", sart_build_file_name="%s", sart_build_path="%s" where sart_build_id="%s"' % \
                      (sw_release, file_name, file_location, sw_build)

    DB_CONN.execute_sql_statement(sql_command)

    LOG._log('INFO: end _update_sart_build')


if __name__ == '__main__':
    DB_CONN = DatabaseConnection(LOG)
    try:
        _update_sw_table(NA_BUILD_BASE, 'NA')
        _update_sw_table(SART_BUILD_BASE, 'SART')
        _update_sw_table(CMS_BUILD_BASE, 'CMS')
    except Exception, e:
        LOG._log('ERROR: %s' % e)
        pass
    finally:
	    DB_CONN.close_database_connection()